﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BuisnessLayer;
using Microsoft.Win32;
using System.IO;

namespace DataEditor.Components
{
    /// <summary>
    /// Interaction logic for SkillDescriptionComponent.xaml
    /// </summary>
    public partial class SkillDescriptionComponent : UserControl
    {
        public EventHandler SkillDataChanged;
        private OccupationsOccupationSkill _skill;
        public SkillDescriptionComponent(OccupationsOccupationSkill pData)
        {
            InitializeComponent();
            _skill = pData;
            LoadData();

        }

        private void LoadData() {
            SkillDescriptionTextBlock.Text = _skill.Description;
            if (_skill.SkillIcon != null && _skill.SkillIcon.Length > 0)
                SkillIcon.Source = ImageUtil.BinaryToImageSource(ImageUtil.StringToByteArray(_skill.SkillIcon));
            lblSkillName.Content = _skill.Name;
            lblSkillType.Content = "Type : " + _skill.Type;
            lblAccessLevel.Content = string.Format("Available at {0} level", _skill.AccessLvl);
        }

        private void SkillDescriptionTextBlock_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            var newDescription = UserInteract.AskStringData(new AskerMessage{
                    ConfirmContent = "Save",
                    Message = "Write a shortt description of a skill.",
                    Title ="Skill description select",
                    DiscardContent = "Cancel"
                });
            SkillDescriptionTextBlock.Text = newDescription;
            _skill.Description = newDescription;
            RaiseEvent();
        }

        private void SkillIcon_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left && e.ClickCount == 2)
            {
                var filedialog = new OpenFileDialog();
                string imagepath = "";
                if (filedialog.ShowDialog().Value)
                {
                    imagepath = filedialog.FileName;
                    byte[] b = ImageUtil.ImageToByteStream(imagepath);
                    _skill.SkillIcon = ImageUtil.ByteArrayToString(b);
                    SkillIcon.Source = ImageUtil.BinaryToImageSource(b);
                    RaiseEvent();
                }
            }
        }

        private void lblSkillName_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left && e.ClickCount == 2)
            {
                var newName = UserInteract.AskStringData(new AskerMessage{
                    ConfirmContent = "Change",
                    Message = "Write new name to skill",
                    Title ="Skill name",
                    DiscardContent = "Cancel"
                });
                lblSkillName.Content = newName;
                _skill.Name = newName;
                RaiseEvent();
            }
        }

        private void lblSkillType_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left && e.ClickCount == 2)
            {
                var newType = UserInteract.AskStringData(new AskerMessage{
                    ConfirmContent = "Change",
                    Message = "Specify skill type.",
                    Title ="Type selection",
                    DiscardContent = "Cancel"
                });
                lblSkillType.Content = "Type : "+newType;
                //TODO: 
                if (newType.ToUpper().StartsWith("A"))
                    _skill.Type = OccupationsOccupationSkillType.Active;
                else
                    _skill.Type = OccupationsOccupationSkillType.Passive;
                RaiseEvent();
                //    _skill.Type = newType;
            }
        }

        private void lblAccessLevel_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left && e.ClickCount == 2)
            {
                var newLevel = UserInteract.AskIntegerData(new AskerMessage{
                    ConfirmContent = "Confirm",
                    Message = "Specify at what level skill will be available.",
                    Title ="Level select",
                    DiscardContent = "Cancel"
                });
                lblAccessLevel.Content = string.Format("Available at {0} level", newLevel);
                _skill.AccessLvl = newLevel.ToString(); 
                RaiseEvent();
            }
        }
        private void RaiseEvent() {
            if (this.SkillDataChanged != null)
                this.SkillDataChanged(_skill, null);
        }

    }
}
